<?php
class Frmcreator_Model_Element extends Core_Model_Item_Abstract
{
    protected $_searchTriggers = false;
    
    public function _delete(){
        //Clean all options related to element
    }
    
    public function getName(){
        $name = "element_" . $this->element_id;
        $type = $this->element_type;
        if($type == 'button'){
            //There's only one Sumit button'
            $name = "sumit";
        } else if($type == "captcha"){
            $name = "captcha";
        } else if($type == 'file'){
            $name = "file_" . $this->element_id;
        }      
        return $name;
    }
    
    public function getType(){
        $type = $this->element_type;
        
        if($type == 'checkbox') {
            $type = 'multiCheckbox';
        } 
        return $type;
    }
    
    public function getOptions(){
        $table = Engine_Api::_()->getDbtable('options', 'frmcreator');
        $select = $table->select();
        $select->where('element_id = ?', $this->element_id);        
        $options = $table->fetchAll($select);
        
        $arr = array();
        if(count($options) > 0){
            foreach($options as $option){
                $arr[$option->option_id] = $option->label;
            }
        }        
        return $arr;
    }
    
    public function getAttributes(){
        $array = array();
        $type = $this->element_type;
        
        $array['label'] = $this->label;
        $array['description'] = $this->description;
        $array['order'] = $this->order;
        $pos = strpos($this->layout_type, 'submit');
        if($type == 'button' && $pos !== false){
            $array['type'] = 'submit';
            $array['ignore'] = true;
        }        
        //Check if element is required        
        if($this->required){
            $array['allowEmpty'] = false;
            $array['required'] = true;
        }
        
        //Check multi options element
        if($type == "select" || $type == 'radio' || $type == 'checkbox' || $type == "multiselect"){
            $array['multiOptions'] = $this->getOptions();
        }
        
        $attributes = Zend_Json::decode($this->attributes); 
        //Dummy Element
        if($type == "dummy"){
            unset($array['label']);
            unset($array['description']);
            $array['content'] = $this->label;
        }
        
        //Special Case for Captcha
        if($type == "captcha"){
            unset($array);
            $array['order'] = $this->order;            
            $array = array_merge($array, Engine_Api::_()->core()->getCaptchaOptions());            
        }
        
        return $array;
    }
    
    public function isValid(){
        return true;
    }
    
    public function isMultiOptionsElement(){        
        $multi = ($this->element_type == 'checkbox' || $this->element_type == 'select' || $this->element_type == 'radio' || $this->element_type == 'multiselect');
        return $multi;
    }
}
